package com.qxzn.app.page.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;

import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.R;
import com.qxzn.app.page.R2;
import com.qxzn.app.page.interfaces.MyInterface;
import com.qxzn.app.page.util.PermissionUtil;
import com.qxzn.app.util.AppUtil;

import butterknife.BindView;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;

/**
 * 扫码界面
 */
public class QrScanActivity extends BaseActivity implements QRCodeView.Delegate, MyInterface.AllPermissionGrantListener {

    @BindView(R2.id.zxv_add)
    ZXingView zxvAdd;

    @Override
    protected int initLayout() {
        return R.layout.activity_qr_scan;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText(getIntent().getStringExtra("title"));
        iv_right.setVisibility(View.VISIBLE);
        iv_right.setImageResource(R.mipmap.icon_get_pic_local);
        iv_right.setOnClickListener(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        zxvAdd.setDelegate(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view.getId() == R.id.iv_right) {

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (needRequestPermission) {
            needRequestPermission = false;
            requestPermission(this, PermissionUtil.READ_EXTERNAL_STORAGE, PermissionUtil.WRITE_EXTERNAL_STORAGE, PermissionUtil.CAMERA);
        }
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        AppUtil.showLog("扫描结果：" + result);
        vibrate();
        Intent intent = new Intent();
        intent.putExtra("scan_result", result);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void onCameraAmbientBrightnessChanged(boolean isDark) {
        // 这里是通过修改提示文案来展示环境是否过暗的状态，接入方也可以根据 isDark 的值来实现其他交互效果
        String tipText = zxvAdd.getScanBoxView().getTipText();
        String ambientBrightnessTip = "\n环境过暗，请打开闪光灯";
        if (isDark) {
            if (!tipText.contains(ambientBrightnessTip)) {
                zxvAdd.getScanBoxView().setTipText(tipText + ambientBrightnessTip);
            }
        } else {
            if (tipText.contains(ambientBrightnessTip)) {
                tipText = tipText.substring(0, tipText.indexOf(ambientBrightnessTip));
                zxvAdd.getScanBoxView().setTipText(tipText);
            }
        }
        AppUtil.showLog("onCameraAmbientBrightnessChanged：" + isDark);
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        AppUtil.showLog("打开相机出错");
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    @Override
    protected void onStart() {
        super.onStart();
        zxvAdd.startCamera(); // 打开后置摄像头开始预览，但是并未开始识别
//        zxvAdd.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT); // 打开前置摄像头开始预览，但是并未开始识别
        zxvAdd.startSpotAndShowRect(); // 显示扫描框，并开始识别
    }

    @Override
    protected void onStop() {
        zxvAdd.stopCamera(); // 关闭摄像头预览，并且隐藏扫描框
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        zxvAdd.onDestroy(); // 销毁二维码扫描控件
        super.onDestroy();
    }

    @Override
    public void onAllPermissionGrant() {
        AppUtil.showLog("权限申请成功");
//        zxvAdd.onDestroy(); // 销毁二维码扫描控件
//        zxvAdd.startCamera(); // 打开后置摄像头开始预览，但是并未开始识别
        zxvAdd.startSpotAndShowRect(); // 显示扫描框，并开始识别
    }
}
